package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.util.AjaxResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @GetMapping("/users/{userId}")
    public AjaxResult getUserById(@PathVariable Integer userId) {
        //条件构造器
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, userId);
        //根据userId查询
        List<User> userList = userService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(userList)){
            return AjaxResult.success("查询成功", userList);
        }else{
            return AjaxResult.error("查询失败");
        }
    }

}
